package com.example.blog.controller;


import com.example.blog.modules.article.dto.ArticleDTO;
import com.example.blog.modules.article.service.ArticleService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<ArticleDTO> createArticle(@Valid @RequestBody ArticleDTO articleDTO) {
        return ResponseEntity.ok(articleService.createArticle(articleDTO));
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<ArticleDTO> updateArticle(@PathVariable Long id, @Valid @RequestBody ArticleDTO articleDTO) {
        return ResponseEntity.ok(articleService.updateArticle(id, articleDTO));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<ArticleDTO> getArticle(@PathVariable Long id) {
        return ResponseEntity.ok(articleService.getArticleById(id));
    }

    @GetMapping
    public ResponseEntity<Page<ArticleDTO>> getAllPublishedArticles(Pageable pageable) {
        return ResponseEntity.ok(articleService.getAllPublishedArticles(pageable));
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<Page<ArticleDTO>> getArticlesByAuthor(@PathVariable Long userId, Pageable pageable) {
        return ResponseEntity.ok(articleService.getArticlesByAuthor(userId, pageable));
    }

    @GetMapping("/search")
    public ResponseEntity<Page<ArticleDTO>> searchArticles(@RequestParam String keyword, Pageable pageable) {
        return ResponseEntity.ok(articleService.searchArticles(keyword, pageable));
    }

    @PutMapping("/{id}/publish")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<ArticleDTO> publishArticle(@PathVariable Long id) {
        return ResponseEntity.ok(articleService.publishArticle(id));
    }

    @PutMapping("/{id}/unpublish")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<ArticleDTO> unpublishArticle(@PathVariable Long id) {
        return ResponseEntity.ok(articleService.unpublishArticle(id));
    }

    @PostMapping("/{id}/view")
    public ResponseEntity<?> incrementViewCount(@PathVariable Long id) {
        articleService.incrementViewCount(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/popular")
    public ResponseEntity<List<ArticleDTO>> getPopularArticles() {
        return ResponseEntity.ok(articleService.getPopularArticles());
    }

    @GetMapping("/{id}/related")
    public ResponseEntity<List<ArticleDTO>> getRelatedArticles(@PathVariable Long id) {
        return ResponseEntity.ok(articleService.getRelatedArticles(id));
    }

    @GetMapping("/category/{categoryId}")
    public ResponseEntity<Page<ArticleDTO>> getArticlesByCategory(
            @PathVariable Long categoryId, 
            Pageable pageable) {
        return ResponseEntity.ok(articleService.getArticlesByCategory(categoryId, pageable));
    }

    @GetMapping("/tag/{tagId}")
    public ResponseEntity<Page<ArticleDTO>> getArticlesByTag(
            @PathVariable Long tagId, 
            Pageable pageable) {
        return ResponseEntity.ok(articleService.getArticlesByTag(tagId, pageable));
    }
} 